#!/bin/bash

help() {
  echo "【ssh登录服务器工具】"
  echo "（mac使用需要安装：brew install grep）"
  echo "示例一（使用默认密码）：sshtools 127.0.0.1"
  echo "示例二（使用指定密码）：sshtools 127.0.0.1 123456"
  echo "示例三（参数二可以是包含ip的地址）：sshtools http://10.50.26.195:18086/note/1879051910225334273 123456"
}

ip=$1
password=${2:-Lw!Kz@MY5#lc}

if [ -z "$ip" ]; then
    help
    exit 1
fi

if [[ $1 =~ ^(http|https):// ]]; then
  ip_address=$(echo "$1" | ggrep -oP '\d+\.\d+\.\d+\.\d+')
  if [ -n "$ip_address" ]; then
    ip=$ip_address
  else
    echo "IP地址异常"
    exit 1
  fi
fi

ssh-keyscan $ip >> ~/.ssh/known_hosts
sshpass -p $password ssh root@$ip